// pages/pay/pay.js
var httpUtil = require('../../utils/httpUtil.js');
var uiUtil = require('../../utils/uiUtil.js');
var util = require('../../utils/util.js');
const app = getApp();
var that;
var time=0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    src:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    let mchkey = options.mchkey;
    let orderNo = options.orderNo||"";
    let currentPrice = options.currentPrice||"";
    let payType = options.payType||"";
    let activeType = options.activeType||"";
    let memKey = app.globalData.memKey||"";
    if (mchkey){
      let src;
      // if (memKey==""){
      src = "https://cdadmin.landdt.cn/h5/pay/index.html?mchkey=" + mchkey + "&orderNo=" + orderNo + "&currentPrice=" + currentPrice + "&payType=" + payType + "&activeType=" + activeType + "&memKey=" + memKey + " &payMainBody=SMALL#wechat_redirect";
      // } else {
      //   src = "https://cdadmin.landdt.cn/h5/pay/pages/microMessenger_pay/mch.html?mchKey=" + mchkey + "&orderNo=" + orderNo + "&currentPrice=" + currentPrice + "&payType=" + payType + "&activeType=" + activeType + "&memKey=" + memKey + " &payMainBody=SMALL#wechat_redirect";
      // }

      that.setData({
        src: src
      })
    } else{
      uiUtil.showToast("未获取到mchkey");
    }
  },
  
  /**
   * h5页面回调
   */
  postMessage(e){
    if(e.detail){
      let orderNoH5=e.detail.data[0].orderNo;
      let iconPageThat=e.detail.data[0].iconPageThat;
      var pages = getCurrentPages();
      var lastPage = pages[0];
      if (lastPage.route == "pages/index/navigationbar/navigationbar") {
        lastPage.setData({
          "icon.pageThat":iconPageThat
        })
        let orderNo=lastPage.data.orderLists.dataList[0].orderNo;
        let orderStatus=lastPage.data.orderLists.dataList[0].orderStatus;
        if(orderNo==orderNoH5){
          if(orderStatus==0){
            lastPage.setData({
              "orderLists.dataList[0].orderStatus": "-1"
            })
            that.getOrderDetails(orderNoH5);
          }
        } else {
          lastPage.refresherrefreshOrder();
        }
        // wx.navigateBack({
        //   delta:100
        // })
      }
    }
  },

  /**
   * 订单详情查询
   */
  getOrderDetails(orderNoH5){
    httpUtil.PostRequest("searchUserOrderDetail", {
      orderNo: orderNoH5
    }, {
        success(res) {
          if(res.orderStatus==0){
            if(time<60000){
              setTimeout(function(){
                time+=2000;
                console.log(time);
                that.getOrderDetails(orderNoH5);
              },time)
            } else {
              lastPage.refresherrefreshOrder();
            }
          } else {
            var pages = getCurrentPages();
            var lastPage = pages[0];
            if (lastPage.route == "pages/index/navigationbar/navigationbar") {
              let orderNo=lastPage.data.orderLists.dataList[0].orderNo;
              if(orderNoH5==orderNo){
                lastPage.setData({
                  "orderLists.dataList[0].orderStatus": res.orderStatus
                })
              } else {
                lastPage.refresherrefreshOrder();
              }
            }
          }
        },
        fail(code, res) {
          uiUtil.showToast(res);
        },
        complete() { }
      })
  },


})